import {dialog} from "electron";
import Server from "../server";
import WsServer from "../server/wsServer";
import {IpcChannel, IpcMainHandle} from "../ipc";

export function useServerHandle(): Pick<
	IpcMainHandle,
	| IpcChannel.StartServer
	| IpcChannel.StopServer
	| IpcChannel.StartWsServer
	| IpcChannel.StopWsServer
> {
	return {
		[IpcChannel.StartServer]: async () => {
			try {
				const serverStatus = await Server.StartServer();
				return serverStatus;
			} catch (error) {
				dialog.showErrorBox("错误", error);
				return "";
			}
		},
		[IpcChannel.StopServer]: async () => {
			try {
				const serverStatus = await Server.StopServer();
				return serverStatus;
			} catch (error) {
				dialog.showErrorBox("错误", error);
				return "";
			}
		},
		[IpcChannel.StartWsServer]: async () => {
			try {
				const serverStatus = await WsServer.StartServer();
				return serverStatus;
			} catch (error) {
				dialog.showErrorBox("错误", error);
				return "";
			}
		},
		[IpcChannel.StopWsServer]: async () => {
			try {
				const serverStatus = await WsServer.StopServer();
				return serverStatus;
			} catch (error) {
				dialog.showErrorBox("错误", error);
				return "";
			}
		},
	};
}
